<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Zipcode extends CI_Controller {
	private $categories = array(
				 			array(
				                "name" => "Restaurants",
				                "slug" => "restaurants",
				                "categoryName" => "Restaurants",
								"categoryCode" => "restaurant",
								"icon"	=> "restaurant"
				            ),
				            array(
				                "name" => "Bars/Lounges",
				                "slug" => "bar-club",
				                "categoryName" => "Restaurants",
								"categoryCode" => "restaurant",
								"icon"	=> "restaurant"
				             ),
				            array(
				                "name" => "Massage",
				                "slug" => "massage",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Facials",
				                "slug" => "facial",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Nail Care",
				                "slug" => "manicure-pedicure",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Tanning",
				                "slug" => "tanning",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Hair Salons",
				                "slug" => "hair-salon",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Waxing",
				                "slug" => "hair-removal",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Spa",
				                "slug" => "spa",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Teeth Whitening",
				                "slug" => "teeth-whitening",
				                "categoryName" => "Medical",
								"categoryCode" => "medical",
								"icon"	=> "medical"
				             ),
				            array(
				                "name" => "Vision and Eye Care",
				                "slug" => "eye-vision",
				                "categoryName" => "Medical",
								"categoryCode" => "medical",
								"icon"	=> "medical"
				             ),
				            array(
				                "name" => "Makeup",
				                "slug" => "makeup",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Pilates",
				                "slug" => "pilates",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Yoga",
				                "slug" => "yoga",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Gym",
				                "slug" => "gym",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Boot Camp",
				                "slug" => "boot-camp",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Men s Clothing",
				                "slug" => "mens-clothing",
				                "categoryName" => "Shopping",
								"categoryCode" => "shopping",
								"icon"	=> "shopping"
				             ),
				            array(
				                "name" => "Women s Clothing",
				                "slug" => "womens-clothing",
				                "categoryName" => "Shopping",
								"categoryCode" => "shopping",
								"icon"	=> "shopping"
				            ),
				            array(
				                "name" => "Groceries",
				                "slug" => "food-grocery",
				                "categoryName" => "Shopping",
								"categoryCode" => "shopping",
								"icon"	=> "shopping"
				            ),
				            array(
				                "name" => "Dessert",
				                "slug" => "treats",
				                "categoryName" => "Shopping",
								"categoryCode" => "shopping",
								"icon"	=> "shopping"
				             ),
				            array(
				                "name" => "Home Services",
				                "slug" => "home-services",
				                "categoryName" => "Services",
								"categoryCode" => "service",
								"icon"	=> "service"
				             ),
				            array(
				                "name" => "Museums",
				                "slug" => "museums",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Wine Tasting",
				                "slug" => "wine-tasting",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Tours",
				                "slug" => "city-tours",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Comedy Clubs",
				                "slug" => "comedy-clubs",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Shows",
				                "slug" => "theater",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Concerts",
				                "slug" => "concerts",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Life Skills Classes",
				                "slug" => "life-skills-classes",
				                "categoryName" => "",
								"categoryCode" => "",
								"icon"	=> ""
				             ),
				            array(
				                "name" => "Golf",
				                "slug" => "golf",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Bowling",
				                "slug" => "bowling",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Sports Tickets",
				                "slug" => "sporting-events",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Skydiving",
				                "slug" => "skydiving",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				            ),
				            array(
				                "name" => "Skiing",
				                "slug" => "skiing",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Dance Classes",
				                "slug" => "dance-classes",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Baby",
				                "slug" => "baby",
				                "categoryName" => "Shopping",
								"categoryCode" => "shopping",
								"icon"	=> "shopping"
				             ),
				            array(
				                "name" => "Kids",
				                "slug" => "kids",
				                "categoryName" => "Shopping",
								"categoryCode" => "shopping",
								"icon"	=> "shopping"
				             ),
				            array(
				                "name" => "College",
				                "slug" => "college",
				                "categoryName" => "Shopping",
								"categoryCode" => "shopping",
								"icon"	=> "shopping"
				             ),
				            array(
				                "name" => "Bridal",
				                "slug" => "bridal",
				                "categoryName" => "Shopping",
								"categoryCode" => "shopping",
								"icon"	=> "shopping"
				             ),
				            array(
				                "name" => "Pets",
				                "slug" => "pets",
				                "categoryName" => "Shopping",
								"categoryCode" => "shopping",
								"icon"	=> "shopping"
				             ),
				            array(
				                "name" => "Dental",
				                "slug" => "dental",
				                "categoryName" => "Medical",
								"categoryCode" => "medical",
								"icon"	=> "medical"
				             ),
				            array(
				                "name" => "Chiropractor",
				                "slug" => "chiropractic",
				                "categoryName" => "Medical",
								"categoryCode" => "medical",
								"icon"	=> "medical"
				             ),
				            array(
				                "name" => "Skin Care",
				                "slug" => "dermatology",
				                "categoryName" => "Medical",
								"categoryCode" => "medical",
								"icon"	=> "medical"
				             ),
				            array(
				                "name" => "Martial Arts",
				                "slug" => "martial-arts",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Fitness Classes",
				                "slug" => "fitness-classes",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Personal Trainer",
				                "slug" => "personal-training",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Photography",
				                "slug" => "photography-services",
				                "categoryName" => "",
								"categoryCode" => "",
								"icon"	=> ""
				             ),
				            array(
				                "name" => "Automotive",
				                "slug" => "automotive-services",
				                "categoryName" => "Automotive",
								"categoryCode" => "automotive",
								"icon"	=> "automotive"
				             ),
				            array(
				                "name" => "Outdoor Adventures",
				                "slug" => "outdoor-adventures",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Gay",
				                "slug" => "gay",
				                "categoryName" => "",
								"categoryCode" => "",
								"icon"	=> ""
				             ),
				            array(
				                "name" => "Jewish",
				                "slug" => "jewish",
				                "categoryName" => "",
								"categoryCode" => "",
								"icon"	=> ""
				             ),
				            array(
				                "name" => "Travel",
				                "slug" => "travel",
				                "categoryName" => "",
								"categoryCode" => "",
								"icon"	=> ""
				            )
						);
	private $localSearchCategories = array(
				 			array(
				                "name" => "Restaurants",
				                "slug" => "restaurants",
				                "categoryName" => "Restaurants",
								"categoryCode" => "restaurant",
								"icon"	=> "restaurant"
				            ),
				            array(
				                "name" => "American Restaurants",
				                "slug" => "restaurants",
				                "categoryName" => "Restaurants",
								"categoryCode" => "restaurant",
								"icon"	=> "restaurant"
				            ),
				             array(
				                "name" => "Breakfast Restaurants",
				                "slug" => "restaurants",
				                "categoryName" => "Restaurants",
								"categoryCode" => "restaurant",
								"icon"	=> "restaurant"
				            ),
				           array(
				                "name" => "Continental Restaurants",
				                "slug" => "restaurants",
				                "categoryName" => "Restaurants",
								"categoryCode" => "restaurant",
								"icon"	=> "restaurant"
				            ),
				            array(
				                "name" => "International Restaurants",
				                "slug" => "restaurants",
				                "categoryName" => "Restaurants",
								"categoryCode" => "restaurant",
								"icon"	=> "restaurant"
				            ),
				            array(
				                "name" => "Italian Restaurants",
				                "slug" => "restaurants",
				                "categoryName" => "Restaurants",
								"categoryCode" => "restaurant",
								"icon"	=> "restaurant"
				            ),
				            
				            array(
				                "name" => "Resorts",
				                "slug" => "hotel",
				                "categoryName" => "Hotels",
								"categoryCode" => "hotel",
								"icon"	=> "hotel"
				             ),
				             array(
				                "name" => "Wedding Venues",
				                "slug" => "hotel",
				                "categoryName" => "Hotels",
								"categoryCode" => "hotel",
								"icon"	=> "hotel"
				             ),
				             array(
				                "name" => "Convention Centers",
				                "slug" => "hotel",
				                "categoryName" => "Hotels",
								"categoryCode" => "hotel",
								"icon"	=> "hotel"
				             ),
				             array(
				                "name" => "Conference Centers",
				                "slug" => "hotel",
				                "categoryName" => "Hotels",
								"categoryCode" => "hotel",
								"icon"	=> "hotel"
				             ),
				              array(
				                "name" => "Hotels  Motels",
				                "slug" => "hotel",
				                "categoryName" => "Hotels",
								"categoryCode" => "hotel",
								"icon"	=> "hotel"
				             ),
				             array(
				                "name" => "Resorts", 
				                "slug" => "hotel",
				                "categoryName" => "Hotels",
								"categoryCode" => "hotel",
								"icon"	=> "hotel"
				             ),
				             array(
				                "name" => "Event Planners", 
				                "slug" => "hotel",
				                "categoryName" => "Hotels",
								"categoryCode" => "hotel",
								"icon"	=> "hotel"
				             ),
				             array(
				                "name" => "Other Lodging", 
				                "slug" => "hotel",
				                "categoryName" => "Hotels",
								"categoryCode" => "hotel",
								"icon"	=> "hotel"
				             ),
				             array(
				                "name" => "Entertainment Venues", 
				                "slug" => "hotel",
				                "categoryName" => "Hotels",
								"categoryCode" => "hotel",
								"icon"	=> "hotel"
				             ),
				            array(
				                "name" => "Bars/Lounges",
				                "slug" => "bar-club",
				                "categoryName" => "Restaurants",
								"categoryCode" => "restaurant",
								"icon"	=> "restaurant"
				             ),
				            array(
				                "name" => "Massage",
				                "slug" => "massage",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Facials",
				                "slug" => "facial",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Nail Care",
				                "slug" => "manicure-pedicure",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Tanning",
				                "slug" => "tanning",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Hair Salons",
				                "slug" => "hair-salon",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Waxing",
				                "slug" => "hair-removal",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Spa",
				                "slug" => "spa",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Teeth Whitening",
				                "slug" => "teeth-whitening",
				                "categoryName" => "Medical",
								"categoryCode" => "medical",
								"icon"	=> "medical"
				             ),
				            array(
				                "name" => "Vision and Eye Care",
				                "slug" => "eye-vision",
				                "categoryName" => "Medical",
								"categoryCode" => "medical",
								"icon"	=> "medical"
				             ),
				            array(
				                "name" => "Makeup",
				                "slug" => "makeup",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Pilates",
				                "slug" => "pilates",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Yoga",
				                "slug" => "yoga",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Gym",
				                "slug" => "gym",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Boot Camp",
				                "slug" => "boot-camp",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Men s Clothing",
				                "slug" => "mens-clothing",
				                "categoryName" => "Shopping",
								"categoryCode" => "shopping",
								"icon"	=> "shopping"
				             ),
				            array(
				                "name" => "Women s Clothing",
				                "slug" => "womens-clothing",
				                "categoryName" => "Shopping",
								"categoryCode" => "shopping",
								"icon"	=> "shopping"
				            ),
				            array(
				                "name" => "Groceries",
				                "slug" => "food-grocery",
				                "categoryName" => "Shopping",
								"categoryCode" => "shopping",
								"icon"	=> "shopping"
				            ),
				            array(
				                "name" => "Dessert",
				                "slug" => "treats",
				                "categoryName" => "Shopping",
								"categoryCode" => "shopping",
								"icon"	=> "shopping"
				             ),
				            array(
				                "name" => "Home Services",
				                "slug" => "home-services",
				                "categoryName" => "Services",
								"categoryCode" => "service",
								"icon"	=> "service"
				             ),
				            array(
				                "name" => "Museums",
				                "slug" => "museums",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Wine Tasting",
				                "slug" => "wine-tasting",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Tours",
				                "slug" => "city-tours",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Comedy Clubs",
				                "slug" => "comedy-clubs",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Shows",
				                "slug" => "theater",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Concerts",
				                "slug" => "concerts",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Life Skills Classes",
				                "slug" => "life-skills-classes",
				                "categoryName" => "",
								"categoryCode" => "",
								"icon"	=> ""
				             ),
				            array(
				                "name" => "Golf",
				                "slug" => "golf",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Bowling",
				                "slug" => "bowling",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Sports Tickets",
				                "slug" => "sporting-events",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Skydiving",
				                "slug" => "skydiving",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				            ),
				            array(
				                "name" => "Skiing",
				                "slug" => "skiing",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Dance Classes",
				                "slug" => "dance-classes",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Baby",
				                "slug" => "baby",
				                "categoryName" => "Shopping",
								"categoryCode" => "shopping",
								"icon"	=> "shopping"
				             ),
				            array(
				                "name" => "Kids",
				                "slug" => "kids",
				                "categoryName" => "Shopping",
								"categoryCode" => "shopping",
								"icon"	=> "shopping"
				             ),
				            array(
				                "name" => "College",
				                "slug" => "college",
				                "categoryName" => "Shopping",
								"categoryCode" => "shopping",
								"icon"	=> "shopping"
				             ),
				            array(
				                "name" => "Bridal",
				                "slug" => "bridal",
				                "categoryName" => "Shopping",
								"categoryCode" => "shopping",
								"icon"	=> "shopping"
				             ),
				            array(
				                "name" => "Pets",
				                "slug" => "pets",
				                "categoryName" => "Shopping",
								"categoryCode" => "shopping",
								"icon"	=> "shopping"
				             ),
				            array(
				                "name" => "Dental",
				                "slug" => "dental",
				                "categoryName" => "Medical",
								"categoryCode" => "medical",
								"icon"	=> "medical"
				             ),
				            array(
				                "name" => "Chiropractor",
				                "slug" => "chiropractic",
				                "categoryName" => "Medical",
								"categoryCode" => "medical",
								"icon"	=> "medical"
				             ),
				            array(
				                "name" => "Skin Care",
				                "slug" => "dermatology",
				                "categoryName" => "Medical",
								"categoryCode" => "medical",
								"icon"	=> "medical"
				             ),
				            array(
				                "name" => "Martial Arts",
				                "slug" => "martial-arts",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Fitness Classes",
				                "slug" => "fitness-classes",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Personal Trainer",
				                "slug" => "personal-training",
				                "categoryName" => "Health & Beauty",
								"categoryCode" => "health-beauty",
								"icon"	=> "health-beauty"
				             ),
				            array(
				                "name" => "Photography",
				                "slug" => "photography-services",
				                "categoryName" => "",
								"categoryCode" => "",
								"icon"	=> ""
				             ),
				            array(
				                "name" => "Automotive",
				                "slug" => "automotive-services",
				                "categoryName" => "Automotive",
								"categoryCode" => "automotive",
								"icon"	=> "automotive"
				             ),
				            array(
				                "name" => "Outdoor Adventures",
				                "slug" => "outdoor-adventures",
				                "categoryName" => "Events & Attractions",
								"categoryCode" => "event-attraction",
								"icon"	=> "event-attraction"
				             ),
				            array(
				                "name" => "Gay",
				                "slug" => "gay",
				                "categoryName" => "",
								"categoryCode" => "",
								"icon"	=> ""
				             ),
				            array(
				                "name" => "Jewish",
				                "slug" => "jewish",
				                "categoryName" => "",
								"categoryCode" => "",
								"icon"	=> ""
				             ),
				            array(
				                "name" => "Travel",
				                "slug" => "travel",
				                "categoryName" => "",
								"categoryCode" => "",
								"icon"	=> ""
				            )
						);
	public function __construct()
	{
		parent::__construct();
		$this->load->helper(array('form', 'url', 'zipcode'));
		$this->config->load('zipcode');
		$this->lang->load('zipcode', 'english');
		$this->load->model('ZipcodeModel');
		$this->load->model('ZipcodeCacheModel');
	}
	public function index()
	{
		$data = array();
		$zipcodeData = $this->_checkDomain();
		
		if($zipcodeData){
			$data['categories'] = $this->categories;
			$data['localSearchCategories'] = $this->localSearchCategories;
			
			$data['googleMapApiKey'] = $this->config->item('google_map_api_key');
			$data['googleMapSensor'] = $this->config->item('google_map_sensor');
			
			/*
			 * get who is my representative
			 */
			$data['whoIsMyRepresentativeData'] = $this->_getWhoIsMyRepresentative($zipcodeData->ZipCode);
			//$data['whoIsMyRepresentativeData'] = $this->_getWhoIsMyRepresentative('00501');
			
			/*
			 * Get education data
			 */
			$data['educationData'] = $this->_getEducationData($zipcodeData->ZipCode);
			/*
			 * Get Area Information
			 */
			$areaInformation = $this->_getAreaInformation($zipcodeData);
			
			$areaInformation = substr($areaInformation, strpos($areaInformation, '<span style="color:blue;">&lt;text xml:space=&quot;preserve&quot;&gt;</span>') + 76, strlen($areaInformation));
			$areaInformation = substr($areaInformation, 0, strpos($areaInformation, '<span style="color:blue;">&lt;/text&gt;</span>'));
			$data['areaInformation'] = $areaInformation;
			
			$data['zipcodeData'] = $zipcodeData;
			$this->load->view('zipcode', $data);
		}else{
			$bgImagesPath = $this->config->item('background_images_path');
			$bgImages = scandir($bgImagesPath);
			$data['bgImagesPath'] = $bgImagesPath;
			$data['bgImages'] = $bgImages;
			
			$this->load->view('index', $data);
		}
	}
	public function find(){
		$zipcode = $this->input->post('zipcode');
		$zipcodeData = $this->ZipcodeModel->getByZipcode($zipcode);
		$result = array();
		if($zipcodeData){
			$result['result'] = true;
			$result['domain'] = 'http://'.$zipcode.'.us';	
			//$result['domain'] = 'http://'.$zipcode.'.com';
		}else{
			$result['result'] = false;
			$result['message'] = $this->lang->line('zipcode:domain_not_found_error_text');
		}
		echo json_encode($result);
	}
	public function getZipcodeData(){
		
		$base_url = base_url();
		if(strpos($base_url, 'www.')){
			$zipcode = substr($base_url, strpos($base_url, 'www.') + 4, 5);
		}else{
			$zipcode = substr($base_url, strpos($base_url, '//') + 2, 5);
		}
		
		$currentZipcodeData = $this->ZipcodeModel->getByZipcode($zipcode);
		
		if(!$currentZipcodeData){
			die();
		}
		
		$result = array();
		$cityState = $this->input->post('zipcode');
		
		$zipcodeData = $this->ZipcodeModel->getByZipcode($cityState);
		if (!$zipcodeData){
			if(strpos($cityState, ",")){
				$zipcodeData = $this->ZipcodeModel->getByCityAndState(trim(substr($cityState, 0, strpos($cityState, ","))), trim(substr($cityState, strpos($cityState, ",") + 1, strlen($cityState))));
			}else{
				$zipcodeData = $this->ZipcodeModel->getByCityAndState($cityState, $currentZipcodeData->State);
			}
		}
		
		if($zipcodeData){
			$result['result'] = true;
			$result['city'] = $zipcodeData->City;
			$result['zipcode'] = $zipcodeData->ZipCode;
			$result['id'] = $zipcodeData->id;
			$result['lat'] = $zipcodeData->Latitude;
			$result['lon'] = $zipcodeData->Longitude;
		}else{
			$result['result'] = false;
			$result['message'] = $this->lang->line('corresponding_geocode_not_found_text');
		}
		echo json_encode($result);
	}
	
	public function getDeals(){
		$lat = $this->input->post('lat');
		$lon = $this->input->post('lon');
		$tags = $this->input->post('tags');
		$filter = $this->input->post('filter');
		$keyword = $this->input->post('keyword');
		
		if($filter == 'search'){
			$local_search_url = $this->config->item('local_search_url');
			$local_search_appid = $this->config->item('local_search_appid');
			$local_search_radius = $this->config->item('local_search_radius');
			$searchData = array();
			for($i = 0; $i < 10; $i++){
				$params = array('appid' => $local_search_appid,
								'latitude' => $lat,
								'longitude' => $lon,
								'query' => $keyword,
								'radius' => $local_search_radius,
								'results'  => 20,
								'start' => $i * 20 + 1,
								'output' => 'json'
						);
				//echo $local_search_url.http_build_query($params);
				
				$ch = curl_init($local_search_url.http_build_query($params));
				$timeout = 5;
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
				curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
				$data = curl_exec($ch);
				curl_close($ch);
				$data = json_decode($data);
				if(isset($data->ResultSet->Result) &&  is_array($data->ResultSet->Result)){
					$searchData = array_merge($searchData, $data->ResultSet->Result);
				}else{
					break;
				}
			}
			echo json_encode($searchData);
			//echo $searchData;
			die();
		}else{
			$yipit_url = $this->config->item('yipit_url');
			$yipit_key = $this->config->item('yipit_key');
			$yipit_radius = $this->config->item('yipit_radius');
			$yipit_source = $this->config->item('yipit_source');
			$params = array('key' => $yipit_key,
							'lat' => $lat,
							'lon' => $lon,
							'tag' => $tags,
							'radius' => $yipit_radius,
							'limit'  => 200,
							'source' => $yipit_source
					);
			//echo $yipit_url.http_build_query($params);
			
			$ch = curl_init($yipit_url.http_build_query($params));
			$timeout = 5;
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
			curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
			$data = curl_exec($ch);
			curl_close($ch);
		}
		//$data = json_decode($data);
		if($filter){
			if($filter == 'today-deals'){
				$data = json_decode($data, true);
				$tempData = array();
				$tempData['meta'] = $data['meta'];
				$tempData['response'] = array();
				$tempData['response']['deals'] = array();
				foreach ($data['response']['deals'] as $item){
					if(calculateDiffHours($item['date_added'], date("Y-m-d H:i:s")) <= 24){
						$tempData['response']['deals'][] = $item;
					}
				}
				echo json_encode($tempData);
			}else if($filter == 'best-deals'){
				echo $data;
				
			}else if($filter == 'search'){
				$data = json_decode($data, true);
				$tempData = array();
				$tempData['meta'] = $data['meta'];
				$tempData['response'] = array();
				$tempData['response']['deals'] = array();
				foreach ($data['response']['deals'] as $item){
					if(strpos(strtolower(" " . $item['business']['name']), strtolower($keyword))){
						$tempData['response']['deals'][] = $item;
					}else if(strpos(strtolower(" " . $item['title']), strtolower($keyword))){
						$tempData['response']['deals'][] = $item;
					}else if(strpos(strtolower(" " . $item['yipit_title']), strtolower($keyword))){
						$tempData['response']['deals'][] = $item;
					}else{
						foreach ($item['tags'] as $tag){
							if(strpos(strtolower(" " . $tag['name']), strtolower($keyword))){
								$tempData['response']['deals'][] = $item;
								break;
							}
						}
					}
				}
				echo json_encode($tempData);
			}
		}else{
			echo $data;
		}
		
		
	}
	
	public function streetView(){
		$lat = $this->input->post('lat');
		$lon = $this->input->post('lon');
		
		$data = array();
		$data['lat'] = $lat;
		$data['lon'] = $lon;
		$this->load->view('streetview', $data);
	}
	
	private function _checkDomain(){
		$base_url = base_url();
		if(strpos($base_url, 'www.')){
			$zipcode = substr($base_url, strpos($base_url, 'www.') + 4, 5);
		}else{
			$zipcode = substr($base_url, strpos($base_url, '//') + 2, 5);
		}
		
		$zipcodeData = $this->ZipcodeModel->getByZipcode($zipcode);
		if($zipcodeData){
			return $zipcodeData;
		}else{
			if(!strpos($base_url, 'zipcode')){
				header("HTTP/1.1 301 Moved Permanently");
				header("Location: http://zipcode.us");
			}else{
				return null;
			}
		}
	}
	private function _getWhoIsMyRepresentative($zipcode){
		$representativeData = $this->ZipcodeCacheModel->getCacheData($zipcode, 'representative');
		if($representativeData == null || $representativeData->date < time() - 11 * 30 * 24 * 60){
			$whoismyrepresentative_url = $this->config->item('whoismyrepresentative_url');
			$whoismyrepresentative_url = $whoismyrepresentative_url.'?zip='.$zipcode.'&output=json';
			$ch = curl_init();
			$timeout = 5;
			curl_setopt($ch,CURLOPT_URL,$whoismyrepresentative_url);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
			curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
			$data = curl_exec($ch);
			curl_close($ch);
			if($representativeData == null){
				$dataSet = array(
					'zipcode' => $zipcode,
					'type' => 'representative',
					'data' => $data,
					'date' => time()
				);
				$this->ZipcodeCacheModel->insertCacheData($dataSet);
			}else{
				$dataSet = array(
					'data' => $data,
					'date' => time()
				);
				$this->ZipcodeCacheModel->updateCacheData($zipcode, 'representative', $dataSet);
			}
		}else{
			$data = $representativeData->data;
		}
		$data = json_decode($data);
		return $data;
	}
	private function _getEducationData($zipcode){
		$educationData = $this->ZipcodeCacheModel->getCacheData($zipcode, 'education');
		
		if($educationData == null || $educationData->date < time() - 30 * 24 * 60){
			$educationApiKey = $this->config->item('education_api_key');
			$educationUrl = $this->config->item('education_url');
			$params=array('key'=>$educationApiKey, 'f'=>'schoolSearch','resf'=>'json', 'zip'=>$zipcode, 'sn'=>'sf', 'v'=>4);
			$ch = curl_init($educationUrl.http_build_query($params));
			curl_setopt($ch,CURLOPT_HEADER, FALSE);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE );
			$data=curl_exec($ch);
			curl_close($ch);
			
			if($educationData == null){
				$dataSet = array(
					'zipcode' => $zipcode,
					'type' => 'education',
					'data' => $data,
					'date' => time()
				);
				$this->ZipcodeCacheModel->insertCacheData($dataSet);
			}else{
				$dataSet = array(
					'data' => $data,
					'date' => time()
				);
				$this->ZipcodeCacheModel->updateCacheData($zipcode, 'education', $dataSet);
			}
		}else{
			$data = $educationData->data;
		}
		$data = json_decode($data);
		return $data;
	}
	private function _getAreaInformation($zipcodeData){
		$headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
        $headers[] = 'Connection: Keep-Alive';
        $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
		$user_agent = 'Mozilla/5.0 (compatible; YourCoolBot/1.0; +http://zipcode.us)';
		$wikipediaUrl = $this->config->item('wikipedia_url');
		//$params=array('action'=>'query', 'prop'=>'revisions', 'titles'=>ucwords(strtolower($city)), 'rvprop'=>'timestamp|content','format'=>'xml');
		$params_check=array('action'=>'query',
					 	'prop'=>'revisions',
						'titles'=>ucwords(strtolower($zipcodeData->City)).',_'.str_replace(' ', '_', ucwords(strtolower($zipcodeData->StateFullName))),
						'rvprop'=>'timestamp|content',
						'format'=>'xml');
		
		$check = curl_init($wikipediaUrl.http_build_query($params_check));
		curl_setopt($check, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($check,CURLOPT_HEADER, FALSE);
		curl_setopt($check, CURLOPT_USERAGENT, $user_agent);
		curl_setopt($check, CURLOPT_RETURNTRANSFER, TRUE );
		$dataCheck=curl_exec($check);
		curl_close($check);
		
		if(strpos($dataCheck, '#REDIRECT [[')){
			$params=array('action'=>'parse', 'page'=> substr($dataCheck,strpos($dataCheck, '#REDIRECT [[') + 12, strpos($dataCheck, ']]') - strpos($dataCheck, '#REDIRECT [[') - 12));
		}else{
			$params=array('action'=>'parse', 'page'=>ucwords(strtolower($zipcodeData->City)).',_'.str_replace(' ', '_', ucwords(strtolower($zipcodeData->StateFullName))));
		}
		//echo $wikipediaUrl.http_build_query($params);
		//die();
		$ch = curl_init($wikipediaUrl.http_build_query($params));
		//$ch = curl_init('http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=Brooklyn&rvprop=timestamp|content&format=xml');
		//$ch = curl_init('http://en.wikipedia.org/w/api.php?action=parse&text={{Brooklyn}}');
		//$ch = curl_init('http://en.wikipedia.org/w/api.php?action=parse&title=Brooklyn&pageid=47384');
		curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($ch,CURLOPT_HEADER, FALSE);
		curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE );
		$data=curl_exec($ch);
		curl_close($ch);
		/*
		$data = simplexml_load_string($data);
		print_r($data);
		die();
		$content = '';
		foreach($data->query->pages->page->revisions->rev as $key => $value) {
		    $content =  $value;
		    break;
		}
		$test = new wikiParser();
		echo ($test->parse($content));
		die();
		*/
		return $data;
		
	}
	
}